
package gui;

import beans.LoginBeans;//Importação da classe LoginBeans
import dao.logindao.LoginDao;
import javax.swing.JOptionPane;

public class CadastraUsuario extends javax.swing.JDialog {
    LoginBeans usuario = new LoginBeans();
    LoginDao logDao = new LoginDao();
    
    public CadastraUsuario(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

 
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jInternalFrame2 = new javax.swing.JInternalFrame();
        jPanel1 = new javax.swing.JPanel();
        nomeUsuario = new javax.swing.JTextField();
        loginUsuario = new javax.swing.JTextField();
        senhaUsuario = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        salvaUsuario = new javax.swing.JButton();
        cancelaUsuario = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBackground(new java.awt.Color(0, 153, 153));
        setResizable(false);

        jInternalFrame2.setBackground(new java.awt.Color(0, 153, 153));
        jInternalFrame2.setTitle("Cadastro de usuários");
        jInternalFrame2.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        jInternalFrame2.setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/images/preferences-desktop-user.png"))); // NOI18N
        jInternalFrame2.setVisible(true);

        nomeUsuario.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        nomeUsuario.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        nomeUsuario.setToolTipText("Insira o nome do usuário que deseja cadastrar no sistema.");

        loginUsuario.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        loginUsuario.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        loginUsuario.setToolTipText("Insira um nome de usuário para acesso ao sistema.");

        senhaUsuario.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        senhaUsuario.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        senhaUsuario.setToolTipText("Insira sua senha de acesso ao sistema.");

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/preferences-desktop-user128.png"))); // NOI18N

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel2.setText("Nome");

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel3.setText("Usuário");

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel4.setText("Senha");

        salvaUsuario.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        salvaUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/document-save2.png"))); // NOI18N
        salvaUsuario.setText("Salvar");
        salvaUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                salvaUsuarioActionPerformed(evt);
            }
        });

        cancelaUsuario.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        cancelaUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/application-exit.png"))); // NOI18N
        cancelaUsuario.setText("Cancelar");
        cancelaUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelaUsuarioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(senhaUsuario, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(loginUsuario, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(nomeUsuario, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(cancelaUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(salvaUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3))
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(31, 31, 31))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(44, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(nomeUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(loginUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(1, 1, 1)
                        .addComponent(senhaUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cancelaUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(salvaUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(78, 78, 78))
        );

        javax.swing.GroupLayout jInternalFrame2Layout = new javax.swing.GroupLayout(jInternalFrame2.getContentPane());
        jInternalFrame2.getContentPane().setLayout(jInternalFrame2Layout);
        jInternalFrame2Layout.setHorizontalGroup(
            jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jInternalFrame2Layout.setVerticalGroup(
            jInternalFrame2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jInternalFrame2)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jInternalFrame2)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void salvaUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_salvaUsuarioActionPerformed
        if (nomeUsuario.getText().equals("") ){                
                JOptionPane.showMessageDialog(null, "O preenchimento do campo nome é obrigatório.", "Mensagem de erro", JOptionPane.WARNING_MESSAGE);
                nomeUsuario.requestFocus();                
                return;
        }
         if (loginUsuario.getText().equals("") ){                
                JOptionPane.showMessageDialog(null, "Preencha o campo usuário.", "Mensagem de erro", JOptionPane.WARNING_MESSAGE);
                loginUsuario.requestFocus();                
                return;
        }
         if (senhaUsuario.getText().equals("") ){                
                JOptionPane.showMessageDialog(null, "Insira uma senha de acesso.", "Mensagem de erro", JOptionPane.WARNING_MESSAGE);
                senhaUsuario.requestFocus();                
                return;
        }
        
        
        usuario.setLogNome(nomeUsuario.getText());           
        usuario.setLogLogin(loginUsuario.getText());       
        usuario.setLogSenha (senhaUsuario.getText());        
        boolean gravou = logDao.grava(usuario); //Passa os dados a serem gravados para o BD
        if (gravou==true){
            JOptionPane.showMessageDialog(null, "Dados gravados com sucesso.", "Mensagem",JOptionPane.PLAIN_MESSAGE);
        }else{
            JOptionPane.showMessageDialog(null, "Erro na gravação dos dados.", "Mensagem de erro",JOptionPane.WARNING_MESSAGE);
        }
        nomeUsuario.setText(""); //Apaga o conteúdo da janela de texto
        loginUsuario.setText(""); //Apaga o conteúdo da janela de texto
        senhaUsuario.setText(""); //Apaga o conteúdo da janela de texto
    }//GEN-LAST:event_salvaUsuarioActionPerformed

    private void cancelaUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelaUsuarioActionPerformed
        dispose();
    }//GEN-LAST:event_cancelaUsuarioActionPerformed

  
    public static void main(String args[]) {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CadastraUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CadastraUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CadastraUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CadastraUsuario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

       
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelaUsuario;
    private javax.swing.JInternalFrame jInternalFrame2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField loginUsuario;
    private javax.swing.JTextField nomeUsuario;
    private javax.swing.JButton salvaUsuario;
    private javax.swing.JPasswordField senhaUsuario;
    // End of variables declaration//GEN-END:variables
}
